package com.dinghui.salary.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dinghui.salary.pojo.User;
import com.dinghui.salary.pojo.dto.SearchSalaryDTO;
import com.dinghui.salary.pojo.Page;
import com.dinghui.salary.pojo.Salary;
import com.dinghui.salary.pojo.dto.SalaryDTO;
import com.dinghui.salary.pojo.vo.SalaryFieldVO;
import com.dinghui.salary.service.SalaryService;
import com.dinghui.salary.utils.ApiResult;
import com.dinghui.salary.utils.LocalUser;
import com.dinghui.salary.utils.PagedGridResult;
import com.sun.corba.se.spi.ior.IdentifiableBase;
import com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.List;
import java.util.Map;


/**
 * @author tanrentao
 * @date 2021-09-06
 * @desc
 */
@RestController
@RequestMapping("/salary")
@Api(value = "SalaryController", tags = "工资表")
public class SalaryController {

    @Reference(version = "2.0.0")
    private SalaryService salaryService;

    @ApiOperation("查询工资列表")
    @PostMapping("/getSalaryList")
    public ApiResult getSalaryList(@RequestBody SearchSalaryDTO dto) {
        User user = LocalUser.getUser();
        PagedGridResult pagedGridResult = salaryService.selectSalaryList(dto, user);
        return ApiResult.success(pagedGridResult);
    }

    @ApiOperation("查询月工资表字段")
    @PostMapping("/getSalaryFiled")
    public ApiResult getSalaryFiled(@RequestBody SearchSalaryDTO dto) {
        List<SalaryFieldVO> list = salaryService.selectSalaryFiled(dto);
        return ApiResult.success(list);
    }

    @ApiOperation("增加或修改")
    @PostMapping("/add")
    public ApiResult add (@RequestBody Map item) throws Exception{
        int i = 0;
        Page dto = new Page();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        dto.setUpdate_time(formatter.format(date));
        dto.setUpdate_by(LocalUser.getUser().getId().toString());
//        int i= salaryService.modify(dto);
//        if(dto.getId() != null){
//            i= service.modify(dto);
//        }else{
//             i= service.add(dto);
//        }
        i = salaryService.modify(item,dto);
        return ApiResult.success(i);
    }

    @ApiOperation("删除")
    @PostMapping("/Delete")
    public ApiResult Delete(@RequestBody Map item) {
        Page dto = new Page();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        dto.setUpdate_time(formatter.format(date));
        dto.setUpdate_by(LocalUser.getUser().getId().toString());
        return ApiResult.success(salaryService.Delete(item,dto));
    }

    @ApiOperation("查询工资类型列表")
    @PostMapping("/FindAll")
    public ApiResult FindAll(@RequestBody Page dto) {
        PagedGridResult pagedGridResult = salaryService.FindAll(dto);
        return ApiResult.success(pagedGridResult);
    }

    @ApiOperation("根据Id查询")
    @PostMapping("/FindById")
    public ApiResult FindById(@RequestBody SalaryDTO dto) {
        Salary salary = new Salary();
        salary.setId(dto.getId());
        return ApiResult.success(salaryService.FindById(salary));
    }

    @ApiOperation("根据年月，公式计算工资")
    @PostMapping("/equationQuery")
    public ApiResult equationQuery(@RequestBody Page dto) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        dto.setUpdate_time(formatter.format(date));
        dto.setUpdate_by(LocalUser.getUser().getId().toString());
        return ApiResult.success(salaryService.equationQuery(dto));
    }
}
